home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / brailler-04b-c / brlr ƒ / Shell ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  10.7 KB  |  389 lines  |  [TEXT/MMCC]

  1. #include "main.h"
  2. #include "apple events.h"
  3. #include "integrity.h"
  4. #include "menus.h"
  5. #include "prefs.h"
  6. #include "environment.h"
  7. #include "error.h"
  8. #include "print meat.h"
  9. #include "brlr.h"
  10. #include "graphics.h"
  11. #include "graphics dispatch.h"
  12. #include "window layer.h"
  13. #include "program globals.h"
  14. #include <AppleEvents.h>
  15. #include <EPPC.h>
  16.  
  17. static    short            gTheCurrentModifiers;
  18.  
  19. void main(void)
  20. {
  21.     Boolean            programIntegrityVerified;
  22.     Boolean            programIntegritySet;
  23.     
  24.     /* do integrity check before anything else; see integrity.c for details */
  25.     programIntegrityVerified=DoIntegrityCheck(&programIntegritySet);
  26.     
  27.     /* standard program initialization stuff */
  28.     MaxApplZone();    
  29.     InitGraf(&qd.thePort);
  30.     InitFonts();
  31.     FlushEvents(everyEvent, 0);
  32.     InitWindows();
  33.     InitMenus();
  34.     TEInit();
  35.     InitDialogs(0L);
  36.     InitCursor();
  37.     GetDateTime((unsigned long*)&qd.randSeed);
  38.     
  39.     InitTheWindowLayer();
  40.     
  41.     if (!InitTheEnvironment())            /* gestalt checks and variable initialization */
  42.         HandleError(kSystemTooOld, TRUE);        /* less than system 4.1 */
  43.     
  44.     if (!programIntegrityVerified)    /* integrity check failed */
  45.         HandleError(kProgramIntegrityNotVerified, TRUE);
  46.     
  47.     if (programIntegritySet)    /* integrity check freshly installed */
  48.         HandleError(kProgramIntegritySet, FALSE);
  49.     
  50.     PrefsError(PreferencesInit());    /* get prefs (create if necessary) */
  51.     
  52.     if (!InitTheMenus())        /* get menus from .rsrc and draw menu bar */
  53.         HandleError(kProgramIntegrityNotVerified, TRUE);
  54.         
  55.     InitThePrinting();
  56.     
  57.     InitTheProgram();
  58.     
  59.     EventLoop();                    /* where it all happens (see below) */
  60.     
  61.     ShutDownEnvironment(TRUE);        /* where it all ends (see below) */
  62.     
  63.     ExitToShell();
  64. }
  65.  
  66. void EventLoop(void)
  67. {
  68.     while (!gDone)    /* gDone set by choosing "Quit" menu item or by "quit" apple event */
  69.         HandleSingleEvent(TRUE);
  70. }
  71.  
  72. Boolean HandleSingleEvent(Boolean allowContextSwitching)
  73. {
  74.     EventRecord        theEvent;
  75.     WindowPtr        front;
  76.     
  77.     if (!gCustomCursor)
  78.         SetCursor(&qd.arrow);
  79.     HiliteMenu(0);            /* normalize menubar */
  80.     
  81.     gFrontWindowIndex=0;
  82.     gFrontWindowIsOurs=FALSE;
  83.     front=FrontWindow();
  84.     if (front!=0L)    /* if there's a front window, see if it's one of ours */
  85.     {
  86.         if (WindowHasLayer(front))
  87.         {
  88.             SetPort(front);
  89.             gFrontWindowIsOurs=TRUE;
  90.             gFrontWindowIndex=GetWindowIndex(front);
  91.         }
  92.     }
  93.         
  94.     /* get an event from the queue */
  95.     WaitNextEvent(everyEvent, &theEvent, gIsInBackground ? gBackgroundWaitTime : gForegroundWaitTime, 0L);
  96.     gTheCurrentModifiers=theEvent.modifiers;
  97.     
  98.     DispatchEvents(theEvent, allowContextSwitching);    /* handle the event we just got */
  99.     
  100.     return (theEvent.what!=nullEvent);
  101. }
  102.  
  103. short GetTheModifiers(void)
  104. {
  105.     return gTheCurrentModifiers;
  106. }
  107.  
  108. void DispatchEvents(EventRecord theEvent, Boolean allowContextSwitching)
  109. {
  110.     Point            thisPoint;
  111.     short            index;
  112.     WindowPtr        theWindow;
  113.     Boolean            thisWindowIsOurs;
  114.     unsigned char    theChar;
  115.     
  116.     thisWindowIsOurs=FALSE;
  117.     /* for update/activate events, see if the window in question is one of ours */
  118.     if ((theEvent.what==activateEvt) || (theEvent.what==updateEvt))
  119.     {
  120.         thisWindowIsOurs=WindowHasLayer((WindowPtr)theEvent.message);
  121.         if (thisWindowIsOurs)
  122.             index=GetWindowIndex((WindowPtr)theEvent.message);
  123.     }
  124.     else if (gFrontWindowIsOurs)    /* if front window is ours, get its window index */
  125.         index=gFrontWindowIndex;
  126.     else index=-1;
  127.     
  128.     switch (theEvent.what)
  129.     {
  130.         case nullEvent:    /* ain't nuthin' happenin' */
  131.             if (gKludgeIter<3)
  132.             {
  133.                 gKludgeIter++;
  134.             }
  135.             else
  136.             {
  137.                 if (gNeedToOpenWindow)
  138.                 {
  139.                     OpenTheIndWindow(kMainWindow);
  140.                     gNeedToOpenWindow=FALSE;
  141.                 }
  142.             }
  143.             
  144.             if (gFrontWindowIsOurs)        /* give control to window dispatch to handle null */
  145.             {
  146.                 thisPoint=theEvent.where;
  147.                 GlobalToLocal(&thisPoint);
  148.                 theWindow=FrontWindow();
  149.                 if ((WindowHasLayer(theWindow)) && (WindowIsFloat(theWindow)))
  150.                 {
  151.                     if (IdleWindowDispatch(index, thisPoint)==kPassThrough)
  152.                     {
  153.                         if ((theWindow=GetFrontDocumentWindow())!=0L)
  154.                         {
  155.                             index=GetWindowIndex(theWindow);
  156.                             SetPort(theWindow);
  157.                             thisPoint=theEvent.where;
  158.                             GlobalToLocal(&thisPoint);
  159.                             IdleWindowDispatch(index, thisPoint);
  160.                         }
  161.                     }
  162.                 }
  163.                 else
  164.                 {
  165.                     IdleWindowDispatch(index, thisPoint);
  166.                 }
  167.             }
  168.             break;
  169.         case mouseDown:    /* mouse button pressed */
  170.             HandleMouseDown(theEvent, allowContextSwitching);    /* see below for mousedown handling */
  171.             break;
  172.         case keyDown:    /* key pressed */
  173.         case autoKey:    /* key help down */
  174.             theChar=(unsigned char)(theEvent.message & charCodeMask);
  175.             
  176.             if (theEvent.modifiers & cmdKey)    /* handle as command-key equivalent */
  177.             {
  178.                 AdjustMenus();    /* just to be safe */
  179.                 HandleMenu(MenuKey(theChar));
  180.             }
  181.             else if (gFrontWindowIsOurs)    /* --> window's dispatch for keydown */
  182.             {
  183.                 theWindow=FrontWindow();
  184.                 if ((WindowHasLayer(theWindow)) && (WindowIsFloat(theWindow)))
  185.                 {
  186.                     if (KeyDownDispatch(index, theChar)==kPassThrough)
  187.                     {
  188.                         if ((theWindow=GetFrontDocumentWindow())!=0L)
  189.                         {
  190.                             index=GetWindowIndex(theWindow);
  191.                             KeyDownDispatch(index, theChar);
  192.                         }
  193.                     }
  194.                 }
  195.                 else
  196.                 {
  197.                     KeyDownDispatch(index, theChar);
  198.                 }
  199.             }
  200.             break;
  201.         case diskEvt:    /* disk insert */
  202.             if (HiWord(theEvent.message)!=noErr)    /* bad disk inserted */
  203.             {
  204.                 DILoad();    /* load disk initialization package */
  205.                 SetPt(&thisPoint, 120, 120);
  206.                 DIBadMount(thisPoint, theEvent.message);    /* give format? dialog */
  207.                 DIUnload();    /* unload 'cuz we certainly don't need it */
  208.             }
  209.             break;
  210.         case updateEvt:    /* window update */
  211.             theWindow=(WindowPtr)theEvent.message;    /* which window? */
  212.             
  213.             BeginUpdate(theWindow);        /* means: "OK, we're dealing with this now" */
  214.             
  215.             if (thisWindowIsOurs)        /* one of ours?  see graphics.c */
  216.                 UpdateTheWindow(theWindow);
  217.             
  218.             EndUpdate(theWindow);        /* means: "OK, we're done updating now" */
  219.             break;
  220.         case activateEvt:    /* window activate or deactivate */
  221.             if (thisWindowIsOurs)        /* one of ours?  send message to window dispatch */
  222.             {
  223.                 if (gIgnoreNextActivateEvent)
  224.                     gIgnoreNextActivateEvent=FALSE;
  225.                 else
  226.                 {
  227.                     if ((theEvent.modifiers & activeFlag)!=0)
  228.                         ActivateWindowDispatch(index);
  229.                     else
  230.                         DeactivateWindowDispatch(index);
  231.                 }
  232.             }
  233.             break;
  234.         case osEvt:            /* suspend or resume program execution (switch in/out) */
  235.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  236.             {
  237.                 /* keep track of whether we're in the background or foreground */
  238.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  239.                 
  240.                 if (gFrontWindowIsOurs)        /* send activate/deactivate to front window */
  241.                 {
  242.                     if (gIsInBackground)
  243.                         DeactivateWindowDispatch(index);
  244.                     else
  245.                         ActivateWindowDispatch(index);
  246.                 }
  247.                 
  248.                 /* if we just came into the foreground and we have a pending error,
  249.                    now's the time to display it */
  250.                 if ((!gIsInBackground) && (gPendingResultCode!=allsWell))
  251.                 {
  252.                     if (gHasNotificationManager)
  253.                         NMRemove(&gMyNotification);        /* remove notification request */
  254.                     HandleError(gPendingResultCode, FALSE);    /* display alert, see error.c */
  255.                     gPendingResultCode=allsWell;        /* ...now it is */
  256.                 }
  257.             }
  258.             break;
  259.         case kHighLevelEvent:    /* apple event */
  260.             AEProcessAppleEvent(&theEvent);        /* see apple events.c */
  261.             break;
  262.     }
  263. }
  264.  
  265. void HandleMouseDown(EventRecord theEvent, Boolean allowContextSwitching)
  266. {
  267.     WindowPtr        theWindow;
  268.     short            windowCode;
  269.     long            windSize;
  270.     GrafPtr            oldPort;
  271.     Rect            sizeRect;
  272.     short            index;
  273.     Point            theLocalPoint;
  274.     Boolean            thisWindowIsOurs;
  275.     
  276.     windowCode=FindWindow(theEvent.where, &theWindow);    /* which window? */
  277.  
  278.     thisWindowIsOurs=FALSE;
  279.     thisWindowIsOurs=WindowHasLayer(theWindow);
  280.     if ((thisWindowIsOurs=WindowHasLayer(theWindow))==TRUE)
  281.         index=GetWindowIndex(theWindow);
  282.     else
  283.         index=-1;
  284.     
  285.     switch (windowCode)
  286.     {
  287.         case inMenuBar:        /* in menu bar; let system take over */
  288.             AdjustMenus();
  289.             HandleMenu(MenuSelect(theEvent.where));
  290.             break;
  291.         case inContent:        /* in window content */
  292.             if (!MySelectWindow(theWindow))        /* didn't need to change front windows */
  293.             {
  294.                 if (gFrontWindowIsOurs)    /* inform window dispatch of mousedown */
  295.                 {
  296.                     theLocalPoint=theEvent.where;
  297.                     SetPort(theWindow);
  298.                     GlobalToLocal(&theLocalPoint);
  299.                     MouseDownDispatch(index, theLocalPoint);
  300.                 }
  301.             }
  302.             break;
  303.         case inSysWindow:    /* in system window (desk accessory) */
  304.             if (allowContextSwitching)
  305.                 SystemClick(&theEvent, theWindow);    /* let the system deal with it */
  306.             break;
  307.         case inDrag:        /* in drag _region_, that is */
  308.             /* the accepted way to draw a window */
  309.             DragWindow(theWindow, theEvent.where, &((**GetGrayRgn()).rgnBBox));
  310.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  311.             {
  312.                 SetWindowBounds(theWindow,
  313.                     (*(((WindowPeek)theWindow)->contRgn))->rgnBBox);
  314.                 theLocalPoint.v=GetWindowBounds(theWindow).top;
  315.                 theLocalPoint.h=GetWindowBounds(theWindow).left;
  316.                 SetWindowTopLeft(theWindow, theLocalPoint);
  317.                 MySelectWindow(theWindow);
  318.             }
  319.             break;
  320.         case inGoAway:        /* close box */
  321.             /* the accepted way to track a close box attempt */
  322.             if (TrackGoAway(theWindow, theEvent.where))
  323.                 DoTheCloseThing((WindowPeek)theWindow);        /* see menus.c */
  324.             break;
  325.         case inGrow:        /* grow box */
  326.             /* the accepted way to grow a window */
  327.             if (thisWindowIsOurs)
  328.             {
  329.                 if (GetGrowSizeDispatch(index, &sizeRect)==kFailure)
  330.                     sizeRect=qd.screenBits.bounds;
  331.             }
  332.             else sizeRect=qd.screenBits.bounds;
  333.             
  334.             windSize=GrowWindow(theWindow, theEvent.where, &sizeRect);
  335.             if (windSize!=0)
  336.             {
  337.                 GetPort(&oldPort);
  338.                 SetPort(theWindow);
  339.                 EraseRect(&theWindow->portRect);
  340.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  341.                 InvalRect(&theWindow->portRect);
  342.                 SetPort(oldPort);
  343.                 
  344.                 if (thisWindowIsOurs)    /* update window bounds in window data struct */
  345.                 {
  346.                     GrowWindowDispatch(index);
  347.                     SetWindowBounds(theWindow,
  348.                         (*(((WindowPeek)theWindow)->contRgn))->rgnBBox);
  349.                     theLocalPoint.v=GetWindowBounds(theWindow).top;
  350.                     theLocalPoint.h=GetWindowBounds(theWindow).left;
  351.                     SetWindowTopLeft(theWindow, theLocalPoint);
  352.                 }
  353.             }
  354.             break;
  355.         case inZoomIn:        /* zoom box */
  356.         case inZoomOut:
  357.             /* the accepted way to track a zoom attempt */
  358.             if (TrackBox(theWindow, theEvent.where, windowCode))
  359.             {
  360.                 GetPort(&oldPort);
  361.                 SetPort(theWindow);
  362.                 ZoomWindow(theWindow, windowCode, FALSE);
  363.                 InvalRect(&theWindow->portRect);
  364.                 SetPort(oldPort);
  365.             }
  366.             
  367.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  368.             {
  369.                 ZoomWindowDispatch(index);
  370.                 SetWindowBounds(theWindow,
  371.                     (*(((WindowPeek)theWindow)->contRgn))->rgnBBox);
  372.                 theLocalPoint.v=GetWindowBounds(theWindow).top;
  373.                 theLocalPoint.h=GetWindowBounds(theWindow).left;
  374.                 SetWindowTopLeft(theWindow, theLocalPoint);
  375.             }
  376.             break;
  377.     }
  378. }
  379.  
  380. void ShutDownEnvironment(Boolean fullShutdown)
  381. {
  382.     SaveThePrefs();
  383.     if (fullShutdown)
  384.     {
  385. //        ShutDownTheProgram();        /* program-specific cleanup */
  386.         ShutDownTheWindowLayer();    /* do shutdown dispatch for all windows we've used */
  387.     }
  388. }
  389.